package com.link2room.blaster.util;

import com.link2room.blaster.constant.BlasterMnsConstant;
import com.link2room.blaster.constant.MsgConstant;
import com.link2room.blaster.dto.innerUse.MsgSendParamDto;
import com.link2room.common.mns.MNSUtil;
import com.link2room.common.util.JacksonUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.link2room.common.util.LogUtil.logger;

/**
 * 消息发送工具
 * Created by LN on 2016/11/9.
 */
@Component("messageFlatPad")
public class MessageFlatPad {
    @Resource
    private MNSUtil mnsUtil;

    /**
     * 预定成功微信消息发送
     * @param lhotelGroupCode
     * @param data
     */
    public void notifyOrderSuccessInWeixin(String lhotelGroupCode,Object data) throws Exception {
        send(lhotelGroupCode, MsgConstant.CHANNEL.WECHAT, MsgConstant.TEMPLET_CODE.ORDER_SUCCESS,data);
    }

    /**
     * 取消订单
     * @param lhotelGroupCode
     * @param data
     * @throws Exception
     */
    public void notifyOrderCancelInWeixin(String lhotelGroupCode,Object data) throws Exception {
        send(lhotelGroupCode, MsgConstant.CHANNEL.WECHAT, MsgConstant.TEMPLET_CODE.CANCEL,data);
    }

    /**
     * 支付成功
     * @param lhotelGroupCode
     * @param data
     * @throws Exception
     */
    public void notifyOrderPaySuccessInWeixin(String lhotelGroupCode,Object data) throws Exception{
        send(lhotelGroupCode, MsgConstant.CHANNEL.WECHAT, MsgConstant.TEMPLET_CODE.PAY_SUCCESS,data);
    }

    /**
     * 发送模板消息消息
     * @param msgParamDto
     */
    public void sendMsg(MsgSendParamDto msgParamDto) throws Exception {
        if(msgParamDto == null){
            logger.debug("msgParamDto is null,no message to send！");
            return;
        }
        if(msgParamDto.getData() == null){
            logger.debug("data is null,no message to send！");
            return;
        }
        String dataToSend = "";
        dataToSend = JacksonUtil.beanToJson(msgParamDto);
        mnsUtil.putMessageToQueue(BlasterMnsConstant.QUEUE_MSG_TO_SEND,dataToSend);

    }
    /**
     * 发送消息
     * @param lhotelGroupCode
     * @param channel
     * @param templataCode
     * @param data
     * @throws Exception
     */
    public void send(String lhotelGroupCode,String channel ,String templataCode , Object data) throws Exception {
        MsgSendParamDto msgSendParamDto = new MsgSendParamDto(lhotelGroupCode, channel, templataCode, data);
        sendMsg(msgSendParamDto);
    }

}
